#include "ArcherBase.h"
#include "GameGlobal.h"
#include <cmath>
using namespace std;

void CArcherBase::SetPos(const MyPoint &ptPos)
{
    
    m_ptPos = ptPos;
    
    UpdateAABB();
}

//-----------------------------------------------------------------------------------------------------/
void CArcherBase::UpdateAABB()
{
    m_aabbBody._ptLeftUp      = MyPoint(m_aabbBodyOrg._ptLeftUp.x + m_ptPos.x,m_aabbBodyOrg._ptLeftUp.y+ m_ptPos.y);
    m_aabbBody._ptRightBottom = MyPoint(m_aabbBodyOrg._ptRightBottom.x + m_ptPos.x,m_aabbBodyOrg._ptRightBottom.y+ m_ptPos.y);
    m_aabbFoot._ptLeftUp      = MyPoint(m_aabbFootOrg._ptLeftUp.x + m_ptPos.x,m_aabbFootOrg._ptLeftUp.y+ m_ptPos.y);
    m_aabbFoot._ptRightBottom = MyPoint(m_aabbFootOrg._ptRightBottom.x + m_ptPos.x,m_aabbFootOrg._ptRightBottom.y+ m_ptPos.y);
    m_aabbHead._ptLeftUp      = MyPoint(m_aabbHeadOrg._ptLeftUp.x + m_ptPos.x,m_aabbHeadOrg._ptLeftUp.y+ m_ptPos.y);
    m_aabbHead._ptRightBottom = MyPoint(m_aabbHeadOrg._ptRightBottom.x + m_ptPos.x,m_aabbHeadOrg._ptRightBottom.y+ m_ptPos.y);
}

//-----------------------------------------------------------------------------------------------------/
HitPositon CArcherBase::IsHitted(MyPoint ptStart, MyPoint ptEnd)
{
    
    if(IsLineInsertSquare(ptStart, ptEnd, m_aabbHead._ptLeftUp, m_aabbHead._ptRightBottom))
    {
        return ARCHER_HIT_HEAD;
    }
    
    
    if(IsLineInsertSquare(ptStart, ptEnd, m_aabbBody._ptLeftUp, m_aabbBody._ptRightBottom))
    {
        return ARCHER_HIT_BODY;
    }
    
    if(IsLineInsertSquare(ptStart, ptEnd, m_aabbFoot._ptLeftUp, m_aabbFoot._ptRightBottom))
    {
        return ARCHER_HIT_FOOT;
    }
    
    
    return ARCHER_HIT_NONE;
}

//-----------------------------------------------------------------------------------------------------/
void CArcherBase::Hitted(HitPositon pos, MyPoint ptHurt)
{
    switch (pos) 
    {
        case ARCHER_HIT_BODY:
        {
            m_hp -= 30;
        }
            break;
            
        case ARCHER_HIT_HEAD:
        {
            m_hp -= 45;
        }
            break;
            
        case ARCHER_HIT_FOOT:
        {
            m_hp -= 25;
        }
            break;
        default:
            break;
    }
    
    m_hurtPos = pos;
}

//-----------------------------------------------------------------------------------------------------/
void CArrowBase::SetParam(float mass, float gravity, float windPower, float arrowWidth)
{
    m_mass         = mass;
    m_gravity      = gravity;
    m_curWindPower = windPower;
    m_arrowWidth   = CGameGlobal::GetInstance()->m_resScale * arrowWidth;
    
}

//-----------------------------------------------------------------------------------------------------/
void CArrowBase::Shoot(float power, MyPoint ptHead, float angle, list<CArcherBase *> *pTargetList)
{
    m_startPower = power;
    float rad = CC_DEGREES_TO_RADIANS(angle);
    m_startRad = rad;
    m_pListTarget = pTargetList;
    m_ptStartPos = MyPoint(ptHead.x , ptHead.y);
}

//-----------------------------------------------------------------------------------------------------/
bool CArrowBase::ProcessLogicTime(float time)
{
    float t2 = time*time;
    float x = (m_startPower* time * cos(m_startRad)) / m_mass - m_curWindPower * t2 *0.5f / m_mass;
    float y = (m_startPower* time * sin(m_startRad)) / m_mass - 0.5*m_gravity * t2;
    
    MyPoint ptCur = MyPoint(m_ptStartPos.x + x, m_ptStartPos.y + y);
    
    if(NULL != m_pListTarget)
    {
        if(!(m_ptCur.x == ptCur.x && m_ptCur.y == ptCur.y))
        {
            for(list<CArcherBase *>::iterator iter = m_pListTarget->begin(); iter != m_pListTarget->end(); iter++)
            {
                CArcherBase *pTarget = (*iter);
                HitPositon pos =  pTarget->IsHitted(m_ptCur, ptCur);
                
                if(pos != ARCHER_HIT_NONE)
                {
                    pTarget->Hitted(pos, ptCur);
                    return true;
                }
                
                if(ptCur.x >= 1024 * 3.5f || ptCur.y < -768*0.5f)
                {
                    return true;
                }
            }
        }
    }
    
    m_ptCur = ptCur;
    
    
    return false;
    
}



